using ColorFillGame.Domain.Entities;
using ColorFillGame.Domain.Repositories;
using MediatR;

namespace ColorFillGame.Application.Queries.Game;

public class GetActiveGamesQueryHandler : IRequestHandler<GetActiveGamesQuery, List<ActiveGameDto>>
{
    private readonly IGameRepository _gameRepository;

    public GetActiveGamesQueryHandler(IGameRepository gameRepository)
    {
        _gameRepository = gameRepository;
    }

    public async Task<List<ActiveGameDto>> Handle(GetActiveGamesQuery request, CancellationToken cancellationToken)
    {
        // 获取所有活动游戏（未开始或进行中的游戏）
        var activeGames = await _gameRepository.GetActiveGamesAsync();

        // 转换为DTO列表
        return activeGames
            .Select(game => new ActiveGameDto
            {
                Id = game.Id,
                Name = game.Name,
                GridSize = game.GridSize,
                MaxPlayers = game.MaxPlayers,
                CurrentPlayers = game.Players.Count
            })
            .ToList();
    }
}